//
//  Fade+ImageView.swift
//  DLuxTravel
//
//  Created by llt on 2018/4/18.
//  Copyright © 2018年 imguider. All rights reserved.
//

import UIKit
import Kingfisher

extension UIImageView {
    
    func setImage(string: String?,placeholder:UIImage) {
        
        guard let url = string else {
            
            self.image = placeholder
            return
        }
        
        let _ = UIImageView.imageCache(for: url)
        
        self.kf.setImage(with: URL(string: url), placeholder: placeholder, options: [.transition(.fade(0.25))], progressBlock: nil, completionHandler: nil)
    }
    
    class func imageCache(for key:String) -> UIImage? {

        if let image = KingfisherManager.shared.cache.retrieveImageInMemoryCache(forKey: key) {
            
            return image
        }
        
        return nil
    }
    
    class func getImageCacheSize(completion: @escaping ((_ size: UInt) -> Void))  {
        
        KingfisherManager.shared.cache.calculateDiskCacheSize(completion: completion)
    }
    
    class func clearCache() {
    
        KingfisherManager.shared.cache.clearMemoryCache()
        KingfisherManager.shared.cache.clearDiskCache()
    }
    
    class func retrieveImage(url:URL,completion:@escaping((UIImage?,Error?)->Void)) {
        
        KingfisherManager.shared.retrieveImage(with: url, options: nil, progressBlock: nil) { (image, error, _, _) in
            
            completion(image,error)
            
        }
    }
}
